package sg.edu.nus.iss.billing.gui;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class GridBagHelper {

	public static final int INSETS_NONE = 0;
	public static final int INSETS_SMALL = 1;
	public static final int INSETS_MEDIUM = 2;
	public static final int INSETS_LARGE = 3;
	
  public static GridBagConstraints makeGridBagConstraints(int gridx, int gridy, int span, int anchor, int fill, Insets inset) {
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.insets = inset;
    constraints.anchor = anchor;
    constraints.fill = fill;
    constraints.gridx = gridx;
    constraints.gridy = gridy;
    if (span != 0) {
      constraints.gridwidth = span;
    }
    return constraints;
  }

	public static GridBagConstraints makeGridBagConstraints(int gridx, int gridy, int span, int anchor, int fill, int insetMode) {
		if (insetMode == INSETS_NONE) {
		  return makeGridBagConstraints(gridx, gridy, span, anchor, fill, new Insets(0, 0, 0, 0));
		} else if (insetMode == INSETS_SMALL) {
		  return makeGridBagConstraints(gridx, gridy, span, anchor, fill, new Insets(1, 1, 1, 1));
		} else if (insetMode == INSETS_MEDIUM) {
		  return makeGridBagConstraints(gridx, gridy, span, anchor, fill, new Insets(3, 3, 3, 3));
		} else if (insetMode == INSETS_LARGE) {
		  return makeGridBagConstraints(gridx, gridy, span, anchor, fill, new Insets(5, 5, 5, 5));
		}
    return null;
	}
	
	public static void addLabel(JPanel panel, String text, int alignment, int gridx, int gridy, int anchor, int span, int insetMode) {
		JLabel label = new JLabel(text, alignment);
		panel.add(label, makeGridBagConstraints(gridx, gridy, span, anchor, GridBagConstraints.HORIZONTAL, insetMode));
	}
	
	public static JRadioButton addRadioButton(JPanel panel, String text, int gridx, int gridy, int span, int insetMode) {
		JRadioButton radioButton = new JRadioButton(text);
		panel.add(radioButton, makeGridBagConstraints(gridx, gridy, span, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetMode));
		return radioButton;
	}

	public static Component addComponent(JPanel panel, Component c, int gridx, int gridy, int span, int insetMode) {
		panel.add(c, makeGridBagConstraints(gridx, gridy, span, GridBagConstraints.WEST, GridBagConstraints.BOTH, insetMode));
		return c;
	}
}
